package com.lianzhihui.romeex.mvp.model;

import android.content.Context;

import com.blankj.utilcode.util.ToastUtils;
import com.lianzhihui.romeex.bean.ConversionCoinPriceBean;
import com.lianzhihui.romeex.bean.ConversionConvetListBean;
import com.lianzhihui.romeex.bean.ConversionIndexBean;
import com.lianzhihui.romeex.bean.UserInfoBean;
import com.lianzhihui.romeex.bean.UserRegisterKFBean;
import com.lianzhihui.romeex.net.APIConstant;
import com.lianzhihui.romeex.net.HttpManager;
import com.lianzhihui.romeex.net.HttpManagerCallback;
import com.lianzhihui.romeex.net.ResultData;
import com.lianzhihui.romeex.utils.GsonUtil;
import com.lianzhihui.romeex.utils.LogUtil;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.mvp.model.base
 * @ClassName: InvitationModellmp
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/9 18:18
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/9 18:18
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class ExchangeFragmentModelImp {


    private Context context;
    ExchangeFragmentModelInterface model;

    public ExchangeFragmentModelImp(Context context, ExchangeFragmentModelInterface model) {
        this.context = context;
        this.model = model;
    }

    /**
     * 兑换首页请求
     */
    public void requestConversionIndex() {
        HttpParams params = new HttpParams();
        new HttpManager(context, new HttpManagerCallback() {
            @Override
            public void onSuccess(ResultData o) {
                if (o.getCode() == 200 && o.getData() != null) {
                    ConversionIndexBean bean = GsonUtil.GsonToBean(o.getData(), ConversionIndexBean.class);
                    model.responseConversionIndexSuccess(bean);
                } else {
                    model.onFailure();
                }
            }

            @Override
            public void onError(ApiException e) {
                model.onFailure();
            }
        }).post(APIConstant.CONVERSION_INDEX, params);
    }

    /**
     * 兑换汇率
     */
    public void requestConversionCoinPrice(String sourceCoinName, String sourceCoinNameCobo, String targetCoinName) {
        HttpParams params = new HttpParams();
        params.put("sourceCoinName", sourceCoinName);
        params.put("sourceCoinNameCobo", sourceCoinNameCobo);
        params.put("targetCoinName", targetCoinName);
        new HttpManager(context, new HttpManagerCallback() {
            @Override
            public void onSuccess(ResultData o) {
                if (o.getCode() == 200 && o.getData() != null) {
                    ConversionCoinPriceBean bean = GsonUtil.GsonToBean(o.getData(), ConversionCoinPriceBean.class);
                    model.responseConversionCoinPriceSuccess(bean);
                } else {
                    model.onFailure();
                }
            }

            @Override
            public void onError(ApiException e) {
                model.onFailure();
            }
        }).post(APIConstant.CONVERSION_COINPRICE, params);
    }

    /**
     * 兑换
     */
    public void requestConversionConverte(String payBalance, String sourceCoinName, String sourceCoinNameCobo, String targetCoinName, String targetCoinNameCobo) {
        HttpParams params = new HttpParams();
        params.put("payBalance", payBalance);
        params.put("sourceCoinName", sourceCoinName);
        params.put("sourceCoinNameCobo", sourceCoinNameCobo);
        params.put("targetCoinName", targetCoinName);
        params.put("targetCoinNameCobo", targetCoinNameCobo);
        new HttpManager(context, new HttpManagerCallback() {
            @Override
            public void onSuccess(ResultData o) {
                if (o.getCode() == 200 && o.getData() != null) {
                    model.responseConversionConverteSuccess();
                } else {
                    if (o.getMsg() != null && !o.getMsg().isEmpty()) {
                        ToastUtils.showShort(o.getMsg());
                    }

                    model.onFailure();
                }
            }

            @Override
            public void onError(ApiException e) {
                ToastUtils.showShort(e.getMessage());
                model.onFailure();
            }
        }).post(APIConstant.CONVERSION_CONVERTE, params);
    }

    /**
     * 兑换记录
     */
    public void requestConversionConvetList(int page, int pageSize) {
        HttpParams params = new HttpParams();
        params.put("page", String.valueOf(page));
        params.put("pageSize", String.valueOf(pageSize));

        new HttpManager(context, new HttpManagerCallback() {
            @Override
            public void onSuccess(ResultData o) {
                if (o.getCode() == 200 && o.getData() != null) {
                    ConversionConvetListBean bean = GsonUtil.GsonToBean(o.getData(), ConversionConvetListBean.class);
                    model.responseConversionConvetListSuccess(bean);
                } else {
                    model.onFailure();
                }
            }

            @Override
            public void onError(ApiException e) {
                model.onFailure();
            }
        }).post(APIConstant.CONVERSION_CONVETLIST, params);
    }

}
